//
//  MapLayersDemoViewController.m
//  FMDemoBaseMap
//
//  Created by fengmap on 16/10/17.
//  Copyright © 2016年 fengmap. All rights reserved.
//
//  图层显示控制
//  简介：地图自有标签标注层、公共设施标注层的显示隐藏；

#import "MapLayersDemoViewController.h"
#import "FMMapKit.h"

@interface MapLayersDemoViewController () <FMKMapViewDelegate>
{
    FMKMapView *_mapView;
}

@end

@implementation MapLayersDemoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 离线地图加载
    NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"jlai-cyds-museum02" ofType:@"fmap"];
    _mapView = [[FMKMapView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64) path:dataPath delegate:self];
    [self.view addSubview:_mapView];

    NSString *themePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"jlai-cyds-museum02/jlai-cyds-museum02.theme"];
    [_mapView setThemeWithLocalPath:themePath];
    
    [self createButton];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)createButton
{
    NSArray *btnNames = @[@"标注", @"设施"];
    for (int i = 0; i < 2; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0+i*SCREEN_WIDTH/2, SCREEN_HEIGHT-44, SCREEN_WIDTH/2-1, 44);
        [btn setTitle:btnNames[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [btn setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor] size:btn.frame.size] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageWithColor:FMDemoColor size:btn.frame.size] forState:UIControlStateSelected];
        btn.layer.shadowColor = [UIColor whiteColor].CGColor;
        btn.layer.shadowOpacity = 0.6;
        btn.layer.shadowOffset = CGSizeMake(1, 1);
        btn.tag = i;
        btn.selected = YES;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
    }
}

#pragma mark - FMKMapViewDelegate
- (void)mapViewDidFinishLoadingMap:(FMKMapView *)mapView {
    NSLog(@"地图加载完成");
}
- (void)mapViewDidFailLoadingMap:(FMKMapView *)mapView withError:(NSError *)error {
    NSLog(@"地图加载失败-%@", error);
}

#pragma mark - 按钮点击
- (void)btnClick:(UIButton *)sender
{
    if (sender.tag == 0) { // 标签
        for (NSString *gid in _mapView.map.groupIDs) {
            // 通过groupID获取地图label层管理对象
            FMKLabelLayer *labelLayer = [_mapView.map getLabelLayerByGroupID:gid];
            labelLayer.hidden = sender.selected;
        }
    }
    else { // 公共设施层
        for (NSString *gid in _mapView.map.groupIDs) {
            // 通过groupID获取地图POI点管理层对象
            FMKFacilityLayer *facilityLayer = [_mapView.map getFacilityLayerByGroupID:gid];
            facilityLayer.hidden = sender.selected;
        }
    }
    
    sender.selected = !sender.selected;
}

@end
